//package com.stud.system.service.impl;
//
//import cn.hutool.core.io.IoUtil;
//import com.fs.common.core.exception.ServiceException;
//import com.fs.common.core.utils.I18nUtil;
//import com.fs.file.config.MinioConfig;
//import com.fs.file.service.FileEngine;
//import com.stud.system.service.FileEngine;
//import io.minio.*;
//import io.minio.http.Method;
//import io.minio.messages.DeleteError;
//import io.minio.messages.DeleteObject;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
///**
// * Minio 文件存储
// *
// * @author Stud
// * @date 2024-05-03
// */
//@Component
//@RequiredArgsConstructor
//@Slf4j
//public class MinioServiceImpl implements FileEngine {
//    private final MinioClient minioClient;
//    private final MinioConfig minioConfig;
//
//    @Override
//    public String upload(MultipartFile sourceFile, String targetFolder, String targetFileName) {
//        log.info("开始往minio上传");
//        String result = targetFolder + "/" + targetFileName;
//        try (InputStream inputStream = sourceFile.getInputStream()) {
//            minioClient.putObject(PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(result).stream(inputStream, sourceFile.getSize(), -1).contentType(sourceFile.getContentType()).build());
//        } catch (Exception e) {
//            throw new ServiceException(I18nUtil.get("message.FailedUploadMinio")+":" + e.getMessage());
//        }
//        return result;
//    }
//
//    @Override
//    public String getDownloadUrl(String fileKey) {
//        GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder().bucket(minioConfig.getBucketName()).object(fileKey).method(Method.GET).expiry(180, TimeUnit.MINUTES).build();
//        try {
//            return minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
//        } catch (Exception e) {
//            throw new RuntimeException("获取文件下载地址失败", e);
//        }
//    }
//
//    @Override
//    public void download(String fileKey, OutputStream outputStream) {
//        try {
//            GetObjectResponse object = minioClient.getObject(GetObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileKey).build());
//            byte[] buffer = new byte[8192];
//            int length;
//            while ((length = object.read(buffer)) > 0) {
//                outputStream.write(buffer, 0, length);
//            }
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//        } finally {
//            IoUtil.close(outputStream);
//        }
//    }
//
//    @Override
//    public boolean remove(String... fileKeys) {
//        if (fileKeys == null || fileKeys.length == 0) {
//            return false;
//        }
//
//        List<DeleteObject> objects = new ArrayList<>();
//        for (String fileKey : fileKeys) {
//            objects.add(new DeleteObject(fileKey));
//        }
//        try {
//            Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(minioConfig.getBucketName()).objects(objects).build());
//            results.forEach(deleteErrorResult -> {
//                try {
//                    String message = deleteErrorResult.get().message();
//                    log.info(message);
//                } catch (Exception e) {
//                    log.error(e.getMessage(), e);
//                }
//
//            });
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            return false;
//        }
//        return true;
//    }
//}
